//
//  JNTabBar.swift
//  SinaWeibo
//
//  Created by 9264 on 16/9/19.
//  Copyright © 2016年 LY9264. All rights reserved.
//

import UIKit

class JNTabBar: UITabBar {
    
    //添加闭包
        var composeButtonClosure: (()->())?

 //MARK:- 懒加载
    private lazy var composeButton: UIButton = {
        
        let button = UIButton()
        //设置图片
        button.setImage(UIImage(named:"tabbar_compose_icon_add"), for: .normal)
        button.setImage(UIImage(named:"tabbar_compose_icon_add_highlighted"), for: .highlighted)
       //设置背景
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), for: .normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), for: .highlighted)
        button.sizeToFit()
        
        button.addTarget(self, action: #selector(composeButtonJump), for: .touchUpInside)
        
        return button
    }()
    
    
    @objc private func composeButtonJump(){
        
        print("就是你了")
        composeButtonClosure?()
        
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setupUI()
    }
    
   private func setupUI()  {
        addSubview(composeButton)
    }
    
    
    override func layoutSubviews() {
        super.layoutSubviews()
        composeButton.centerX = width / 2
        composeButton.centerY = height / 2

        let itemWidth = width / 5
    
        var index = 0

        for value in subviews {
            

            if value.isKind(of: NSClassFromString("UITabBarButton")!)  {
                value.width = itemWidth
                value.x = CGFloat(index) * itemWidth
                
                index += 1

                if index == 2 {
                    index += 1
                }
                
            }
            
        }

        
    }

    
    

}





